package net.ziyoushu.demo1;

import net.ziyoushu.demo1.annotation.EnableRocketMq;
import net.ziyoushu.demo1.holder.AsynMessageProduceHoler;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Calendar;

@SpringBootApplication
@Controller
@EnableRocketMq
public class Demo1Application implements CommandLineRunner {
    @Resource
    AsynMessageProduceHoler asynMessageProducer;

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

    }

    @RequestMapping("/index")
    @ResponseBody
    public String sendMessage() throws Exception{
        Message msg = new Message("TopicTest" /* Topic */,
                "TagA" /* Tag */,
                ("lcb消息"+ Calendar.getInstance().getTimeInMillis()).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
        );
        //Call send message to deliver message to one of brokers.
        SendResult sendResult = asynMessageProducer.send(msg);
        System.out.printf("%s%n", sendResult);
        return "success";
    }

    @RequestMapping("/index2")
    @ResponseBody
    public String sendMessage2() throws Exception{
        Message msg = new Message("TopicTest1" /* Topic */,
                "TagA" /* Tag */,
                ("lcb消息"+ Calendar.getInstance().getTimeInMillis()).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
        );
        //Call send message to deliver message to one of brokers.
        SendResult sendResult = asynMessageProducer.send(msg);
        System.out.printf("%s%n", sendResult);
        return "success";
    }

    @RequestMapping("/index3")
    @ResponseBody
    public String sendMessage3() throws Exception{
        Message msg = new Message("TopicTest" /* Topic */,
                "TagB" /* Tag */,
                ("lcb消息"+ Calendar.getInstance().getTimeInMillis()).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
        );
        //Call send message to deliver message to one of brokers.
        SendResult sendResult = asynMessageProducer.send(msg);
        System.out.printf("%s%n", sendResult);
        return "success";
    }

}
